package com.pig4cloud.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.mall.product.dto.AttrParam;
import com.pig4cloud.mall.product.entity.ProductAttribute;
import com.pig4cloud.mall.product.entity.ProductAttributeCategory;
import com.pig4cloud.mall.product.mapper.ProductAttributeCategoryMapper;
import com.pig4cloud.mall.product.mapper.ProductAttributeMapper;
import com.pig4cloud.mall.product.service.ProductAttributeService;
import com.pig4cloud.mall.product.vo.AttrVo;
import com.pig4cloud.mall.product.vo.SkuRuleVo;
import com.pig4cloud.pig.common.core.util.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品属性参数表
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@Service
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeMapper, ProductAttribute> implements ProductAttributeService {

	@Resource
	private ProductAttributeCategoryMapper attrCategoryMapper;

	@Override
	public PageResult listAttr(Long cid, Integer type, Integer current, Integer size) {
		QueryWrapper<ProductAttribute> wrapper = new QueryWrapper<>();
		wrapper.eq("type", type);
		wrapper.eq("product_attribute_category_id", cid);
		IPage<ProductAttribute> iPage = baseMapper.selectPage(new Page<>(current, size), wrapper);
		return PageResult.buildPage(iPage);
	}

	@Override
	public int create(AttrParam attrParam) {
		ProductAttribute attr = new ProductAttribute();
		BeanUtils.copyProperties(attrParam, attr);

		int count = baseMapper.insert(attr);
		// 新增商品属性以后需要更新商品属性分类数量
		ProductAttributeCategory attrCategory = attrCategoryMapper.selectById(attr.getProductAttributeCategoryId());
		if (attr.getType() == 0) {
			attrCategory.setAttributeCount(attrCategory.getAttributeCount() + 1);
		} else if (attr.getType() == 1) {
			attrCategory.setParamCount(attrCategory.getParamCount() + 1);
		}
		attrCategoryMapper.updateById(attrCategory);
		return count;
	}

	@Override
	public ProductAttribute getItem(Long id) {
		return baseMapper.selectById(id);
	}

	@Override
	public int update(Long id, AttrParam attrParam) {
		ProductAttribute attr = new ProductAttribute();
		BeanUtils.copyProperties(attrParam, attr);
		attr.setId(id);
		return baseMapper.updateById(attr);
	}

	@Override
	public int delete(Long id) {
		return baseMapper.deleteById(id);
	}

	@Override
	public SkuRuleVo getSkuRule(Long cid) {
		List<ProductAttribute> attrList = baseMapper.selectList(new QueryWrapper<ProductAttribute>()
				.eq("type", 0)
				.eq("product_attribute_category_id", cid));
		ProductAttributeCategory attrCategory = attrCategoryMapper.selectById(cid);
		SkuRuleVo skuRuleVo = new SkuRuleVo();
		skuRuleVo.setId(attrCategory.getId());
		skuRuleVo.setName(attrCategory.getName());
		List<AttrVo> attrVoList = new ArrayList<>();
		attrList.forEach(item->{
			AttrVo attrVo = new AttrVo();
			attrVo.setName(item.getName());
			attrVo.setInputValue(item.getInputList());
			attrVo.buildArray();
			attrVoList.add(attrVo);
		});
		skuRuleVo.setList(attrVoList);
		return skuRuleVo;
	}
}
